#include "ip2region.h"
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

int main(int, char**) {
    printf("hello world\n");
    ip2region_entry e;
    if (!ip2region_create(&e, "/home/duoduo/ip2region.db"))
    {
        fprintf(stderr, "failed to create entry.\n");
        return 1;
    }
    string str;
    while (true) {
        cout << "please enter a legal IPv4 addr.(enter q/Q to quit):" << std::endl;
        cin >> str;
        if (str == "q" || str == "Q")
        {
            break;
        }
        uint_t ipv4 = ip2long(str.c_str());
        datablock_entry blk;
        if (!ip2region_btree_search(&e, ipv4, &blk)) {
            printf("Not found.\n");
        } else {
            printf("city id: %u, region: %s\n", blk.city_id, blk.region);
        }
    }
    ip2region_destroy(&e);
    return 0;
}
